import axios from 'axios'

// 订单API
export default {
  // 获取所有订单
  getAllOrders() {
    return axios.get('/orders')
  },
  
  // 获取订单详情
  getOrder(id) {
    return axios.get(`/orders/${id}`)
  },
  
  // 根据客户ID获取订单
  getOrdersByCustomer(customerId) {
    return axios.get(`/orders/customer/${customerId}`)
  },
  
  // 根据状态获取订单
  getOrdersByStatus(status) {
    return axios.get(`/orders/status/${status}`)
  },
  
  // 创建订单
  createOrder(order) {
    // 确保数值类型正确
    const orderData = {
      ...order,
      totalAmount: parseFloat(order.totalAmount),
      items: order.items.map(item => ({
        ...item,
        quantity: parseInt(item.quantity),
        unitPrice: parseFloat(item.unitPrice || 0)
      }))
    };
    
    return axios.post('/orders', orderData);
  },
  
  // 更新订单状态
  updateOrderStatus(id, status) {
    return axios.put(`/orders/${id}/status/${status}`)
  },
  
  // 删除订单
  deleteOrder(id) {
    return axios.delete(`/orders/${id}`)
  },
  
  // 获取订单项
  getOrderItems(orderId) {
    return axios.get(`/orders/${orderId}/items`)
  },
  
  // 添加订单项
  addOrderItem(orderId, item) {
    return axios.post(`/orders/${orderId}/items`, item)
  },
  
  // 更新订单项
  updateOrderItem(orderId, itemId, item) {
    return axios.put(`/orders/${orderId}/items/${itemId}`, item)
  },
  
  // 删除订单项
  deleteOrderItem(orderId, itemId) {
    return axios.delete(`/orders/${orderId}/items/${itemId}`)
  }
} 